package org.finesys.system.api.entity;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.IndexedColors;
import org.finesys.common.core.annotation.Excel;
import org.finesys.common.mp.core.base.BaseEntity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.fasterxml.jackson.annotation.JsonIgnore;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

/**
 * 用户对象操作实例
 */

@Accessors(chain = true)
@EqualsAndHashCode(callSuper = true)
@Data
public class SysUser extends BaseEntity {
    /**
     * 默认密码
     */
    public static final String DEFAULT_PASSWORD = "123456";

    // 用户ID
    @TableId(value = "user_id", type = IdType.ASSIGN_ID)
    @Schema(description = "主键id")
    private Long userId;

    // 租户ID
    @Schema(title = "租户ID")
    private Long tenantId;

    // 部门 ID
    @Schema(title = "部门ID")
    private Long deptId;

    // 用户登录名
    @Schema(title = "登录名")
    @Excel(name = "登录名", required = true, headerColor = IndexedColors.RED)
    private String username;

    // 用户密码
    @Schema(description = "密码")
    private String password;
    /**
     * 随机盐
     */
    @JsonIgnore
    @Schema(description = "随机盐")
    private String salt;
    // 用户名称
    @Schema(description = "用户名称")
    @Excel(name = "用户名称", required = true, headerColor = IndexedColors.RED)
    private String name;

    // 邮箱
    @Schema(description = "邮箱")
    private String email;

    // 电话
    @Schema(description = "手机号")
    @Excel(name = "手机号", required = true, headerColor = IndexedColors.RED)
    private String phone;


    // 头像
    @Schema(description = "头像地址")
    private String avatar;

    // 性别
    @Schema(description = "性别")
    private String sex;

    /**
     * 状态 0:正常,9:已锁定
     */
    @Schema(description = "状态")
    private String status;

    // 最近访问IP
    @Schema(description = "最近访问IP")
    private String loginIp;

    // 最近访问时间
    @Schema(description = "最近访问时间")
    private Date lastLoginTime;

    // 描述
    @Schema(description = "描述")
    private String remarks;

    // 密码过期日期
    @Schema(description = "密码过期日期")
    private Date passwordExpireDate;
    // 员工号
    @Schema(description = "员工号")
    private String userSn;

    // 是否管理员
    @TableField(exist = false)
    private String isAdmin;
    /**
     * 0-正常，1-删除
     */
    @TableLogic
    private String delFlag;

    /**
     * 获取字段信息
     */
    public List<Map<String, Object>> getColumns() {
        List<Map<String, Object>> dataColumns = new ArrayList<>();
        Field[] fields = this.getClass().getDeclaredFields();
        //展示顺序
        int seq = 0;
        for (Field field : fields) {
            Map<String, Object> map = new HashMap<>();
            String label = field.getName();
            if (field.isAnnotationPresent(Schema.class)) {
                Schema schema = field.getAnnotation(Schema.class);
                if (schema != null) {
                    label = schema.name();
                }
            }
            map.put("seq", seq++);
            map.put("label", label);
            map.put("columnName", field.getName());
            map.put("visible", true);
            dataColumns.add(map);
        }
        return dataColumns;
    }
}
